package com.itheima.mall.service.umsService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.mall.mapper.umsMapper.UmsRoleMenuRelationMapper;
import com.itheima.pojo.ums.UmsRoleMenuRelation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class UmsRoleMenuRelationApiImpl implements UmsRoleMenuRelationApi{

    @Autowired
    private UmsRoleMenuRelationMapper umsRoleMenuRelationMapper;

    /**
     * 给角色分配菜单
     * @param menuIds 分配的菜单数组
     * @param roleId 角色ID
     * @return
     */
    @Override
    public void allocMenu(ArrayList<Long> menuIds, Long roleId) {
        //删除原始已分配菜单
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId,roleId);
        umsRoleMenuRelationMapper.delete(queryWrapper);
        //添加新分配菜单
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation umsRoleMenuRelation =new UmsRoleMenuRelation();
            umsRoleMenuRelation.setRoleId(roleId);
            umsRoleMenuRelation.setMenuId(menuId);
            umsRoleMenuRelationMapper.insert(umsRoleMenuRelation);
        }
    }

    /**
     * 根据角色ID查询关联菜单IDS数组
     * @param roleId 角色ID
     * @return
     */
    @Override
    public ArrayList<Long> getmenuIds(long roleId) {
        //条件查询
        LambdaQueryWrapper<UmsRoleMenuRelation> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleMenuRelation::getRoleId,roleId);
        List<UmsRoleMenuRelation> umsRoleMenuRelations = umsRoleMenuRelationMapper.selectList(queryWrapper);
        //处理原始数据并返回
        ArrayList<Long> res =new ArrayList<>();
        umsRoleMenuRelations.forEach((umsRoleMenuRelation)->{
            res.add(umsRoleMenuRelation.getMenuId());
        });
        return res;
    }
}
